package com.zhang.service.impl;

import com.zhang.dao.LoveDao;
import com.zhang.dao.UserDao;
import com.zhang.dao.impl.LoveDaoImpl;
import com.zhang.dao.impl.UserDaoImpl;
import com.zhang.pojo.Love;
import com.zhang.pojo.User;
import com.zhang.service.UserService;

import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();
    private LoveDao loveDao = new LoveDaoImpl();

    @Override
    public void regiest(User user) {
        //保存user
        long userId = userDao.save(user);

        //保存love
        List<Integer> ids = user.getLove();
        for (Integer id : ids) {
            userDao.saveUserLove(userId,id);
        }


    }

    @Override
    public User login(String username, String password) {
        return userDao.login(username, password);
    }

    @Override
    public User findUser(int userID) {
        //1.查询user
        User user = userDao.findUser(userID);
        //2.查询love的id集合
        List<Love> loves = loveDao.findLove(userID);
        ArrayList<Integer> list1 = new ArrayList<>();
        for (Love love : loves) {
            list1.add(love.getId());
        }
        user.setLove(list1);
        return user;
    }
}









